﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using KylinORM.Utils;
using System.Text.RegularExpressions;

namespace KylinORM.Aspect.Definition
{
    /// <summary>
    /// 默认实体的映射规则
    /// </summary>
    public class EntityDefinitionRule : IEntityDefinitionRule
    {
        /// <summary>
        /// 读取实体的默认设置
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <returns></returns>
        public EntityInfo GetEntityDefinition(Type entityType)
        {
            EntityInfo entity = new EntityInfo();
            entity.TableName = this.TableName;
            entity.JoinMethod = EJoinMethod.None;
            //设置表名
            if (string.IsNullOrEmpty(this.TableName))
            {
                //从类名分析表名
                string entityName = entityType.Name;
                Regex rxEntityName = new Regex(@"^(\S+)?(Info|Entity){0,1}$", RegexOptions.RightToLeft);
                Match match = rxEntityName.Match(entityName);
                if (match.Success)
                    entity.TableName = match.Groups[1].Value;
                else
                    entity.TableName = entityName;
            }
            return entity;
        }
        /// <summary>
        /// 读取实体字段的默认设置
        /// </summary>
        /// <param name="propType">实体字段类型</param>
        /// <returns></returns>
        public EntityFieldInfo GetEntityFieldDefinition(PropertyInfo propType)
        {
            EntityFieldInfo field = new EntityFieldInfo();
            field.FieldName = propType.Name;
            field.SelectMethod = ESelectMethod.Equal;
            field.PropertyInfo = propType;
            //检查当前字段是否忽略
            if (this.IgnoredFields != null && this.IgnoredFields.Contains(field.FieldName))
            {
                field.IsIgnoredField = true;
                return field;
            }
            //将第一个字段设为主键
            if (this.PrimaryFields == null)
            {
                this.PrimaryFields = new string[] { field.FieldName };
            }
            if (this.PrimaryFields != null && this.PrimaryFields.Contains(field.FieldName))
            {
                field.IsPrimaryField = true;
            }
            //将第一个主键字段设为索引键
            if (this.IdentityFields == null && field.IsPrimaryField == true && TypeConverter.IsNumberType(field.PropertyInfo.PropertyType))
            {
                this.IdentityFields = new string[] { field.FieldName };
            }
            if (this.IdentityFields != null && this.IdentityFields.Contains(field.FieldName))
            {
                field.IsIdentityField = true;
            }
            return field;
        }

        #region 可选参数
        /// <summary>
        /// 可选参数，设置实体的数据库表名
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// 可选参数，设置实体的忽略字段
        /// </summary>
        public string[] IgnoredFields { get; set; }
        /// <summary>
        /// 可选参数，设置实体的主键字段
        /// </summary>
        public string[] PrimaryFields { get; set; }
        /// <summary>
        /// 可选参数，设置实体的索引字段
        /// </summary>
        public string[] IdentityFields { get; set; }
        #endregion

    }
}
